import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * The type Lottery.
 */
public class Lottery {
    /**
     * The Lottery max.
     */
    public int lotteryMax;
    /**
     * The Winning lottery.
     */
    public MySet winningLottery;
    /**
     * The Players.
     */
    public ArrayList<Player> players;

    /**
     * Instantiates a new Lottery.
     */
////////////////////////////////////
    public Lottery() {
        this.winningLottery = new MySet();
    }

    /**
     * Instantiates a new Lottery.
     *
     * @param lotteryMax the lottery max
     */
    public Lottery(int lotteryMax) {
        this.lotteryMax = lotteryMax;
        this.winningLottery = new MySet();
    }

    /**
     * Gets winning lottery.
     *
     * @return the winning lottery
     */
////////////////////////////////////
    public MySet getWinningLottery() {
        return winningLottery;
    }

    /**
     * Sets winning lottery.
     *
     * @param winningLottery the winning lottery
     */
    public void setWinningLottery(MySet winningLottery) {
        this.winningLottery = winningLottery;
    }

    /**
     * Gets lottery max.
     *
     * @return the lottery max
     */
    public int getLotteryMax() {
        return lotteryMax;
    }

    /**
     * Sets lottery max.
     *
     * @param lotteryMax the lottery max
     */
    public void setLotteryMax(int lotteryMax) {
        this.lotteryMax = lotteryMax;
    }


    /**
     * Lottery turn int.
     * Generate lottery results
     *
     * @param p the player
     * @return the prize user got
     */
////////////////////////////////////
    public int lotteryTurn(Player p) {

        p.setWinningSet(p.getUserLottery().intersection(winningLottery));
        if (p.getWinningSet().getCardinality() == 0) {
            System.out.println(p.getName() + " didn't guess anything right.");
        } else {
            System.out.print(p.getName() + "  guessed correctly the numbers: ");
            p.getWinningSet().printSet();
        }
        switch (p.getWinningSet().getCardinality()) {
            case 3:
                System.out.println("won 25 pounds");
                return 25;
            case 4:
                System.out.println("won 100 pounds");
                return 100;
            case 5:
                System.out.println("won 1,000 pounds");
                return 1000;
            case 6:
                System.out.println("Wow!! You guessed all the numbers correctly and won 1,000,000 pounds!!!");
                return 1000000;
            default:
                System.out.println("Too few numbers were guessed correctly, didn't win anything");
                return 0;
        }
    }

    /**
     * Use lottery system.
     * Control the operation of the entire lottery system from requesting the number of users, user names, maximum numbers, lottery numbers for each person and lottery results
     */
    public void useLotterySystem() {
        Scanner sc = new Scanner(System.in);
        Random r = new Random();

        System.out.println("Welcome to the Lottery Simulation System!!");
        int playerNumber;
        while (true) {
            System.out.println("Please enter the number of players :");
            if (sc.hasNextInt()) {
                playerNumber = sc.nextInt();
                sc.nextLine();
                if (playerNumber > 0) {
                    break;
                } else {
                    System.out.println("The number of players must be greater than or equal to 1.");
                }
            } else {
                System.out.println("That's not a valid number. Please enter a integer.");
                sc.next();
            }
        }
        players = new ArrayList<>();
        for (int i = 0; i < playerNumber; i++) {
            while (true) {
                System.out.println("Enter player " + (i + 1) + "'s name:");
                String name = sc.nextLine();
                if (name == null) {
                    System.out.println("The player's name must not be empty.");
                } else {
                    Player player = new Player(name);
                    players.add(player);
                    break;
                }
            }
        }
        int maxLottery;
        while (true) {
            System.out.println("Please enter the maximum number(integer) for the lottery.");
            if (sc.hasNextInt()) {
                maxLottery = sc.nextInt();
                sc.nextLine();
                if (maxLottery > 5) {
                    break;
                } else {
                    System.out.println("Please enter a number greater than or equal to six" + "(since each lottery ticket needs to include six different winning numbers)");
                }
            } else {
                System.out.println("That's not a valid number. Please enter a integer.");
                sc.next();
            }
        }

        lotteryMax = maxLottery;
        System.out.println("Now the game begins, each lottery ticket costs 2 pounds, players can choose six winning numbers per ticket.");
        for (int i = 0; i < playerNumber; i++) {
            boolean lotteryValid = false;
            while (!lotteryValid) {
                System.out.println("Please " + players.get(i).getName() + " enter the lottery number(integer), separated by a space");
                players.get(i).setUserLottery(new MySet());
                String lottery = sc.nextLine();
                String[] lotteryNumbers = lottery.split(" ");
                if (lotteryNumbers.length != 6) {
                    System.out.println("You must enter six different integers.");
                    continue;
                }
                boolean isNum = true;
                for (String num : lotteryNumbers) {
                    try {
                        int number = Integer.parseInt(num);
                        if (number > lotteryMax) {
                            System.out.println("The lottery numbers exceed the maximum value.");
                            isNum = false;
                            break;
                        } else if (number <= 0) {
                            System.out.println("The lottery numbers must be greater than 0.");
                            isNum = false;
                            break;
                        }
                        players.get(i).getUserLottery().addToSet(number);
                    } catch (NumberFormatException e) {
                        System.out.println("This is an incorrect lottery ticket; please enter integer.");
                        isNum = false;
                        break;
                    }
                }
                if (players.get(i).getUserLottery().getCardinality() != 6 && isNum) {
                    System.out.println("You must enter six different integer.");
                } else if (isNum) {
                    lotteryValid = true;
                }
            }
        }
        int weekNumber;
        while (true) {
            System.out.println("Please set how many weeks you would like to purchase with these SAME lottery numbers.");
            if (sc.hasNextInt()) {
                weekNumber = sc.nextInt();
                sc.nextLine();
                if (weekNumber > 0) {
                    break;
                } else {
                    System.out.println("The number you have input is not valid. Please enter a number between 2 and 4.");
                }
            } else {
                System.out.println("That's not a valid number.");
                sc.next();
            }
        }
        for (int i = 0; i < weekNumber; i++) {
            winningLottery.clear();
            int cnt = 0;
            while (cnt < 6) {
                int randomInt = r.nextInt(lotteryMax) + 1;
                if (!winningLottery.isInSet(randomInt)) {
                    winningLottery.addToSet(randomInt);
                    cnt++;
                }
            }
            System.out.println("---------------- Week " + (i + 1) + " -----------------------");
            System.out.print("The winning numbers for the " + (i + 1) + "th week are： ");
            winningLottery.printSet();
            for (int j = 0; j < playerNumber; j++) {
                players.get(j).setCast(players.get(j).getCast() + 2);
                int prize = lotteryTurn(players.get(j));
                players.get(j).setPrize(players.get(j).getPrize() + prize);
            }
        }
        System.out.println("---------------Statistical table---------------");
        for (int i = 0; i < playerNumber; i++) {
            System.out.println("Name" + "\t\t\t" + "Total prize" + "\t\t\t" + "Total cast");
            System.out.println(players.get(i).getName() + "\t\t\t\t" + players.get(i).getPrize() + "\t\t\t\t\t" + players.get(i).getCast());
        }

    }


}
